package com.oinux.playhelper.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.oinux.playhelper.R;
import com.oinux.playhelper.app.AppContext;
import com.oinux.playhelper.app.BaseActivity;
import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UmengUpdateListener;
import com.umeng.update.UpdateResponse;

public class AppAbout extends BaseActivity implements OnClickListener {

	private static final String TAG = "app_about_activity";

	private TextView titleText;
	private TextView appVersionText;
	private Button checkUpdateBtn;
	private TextView copyrightText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about);
		initView();
		initData();
	}

	private void initData() {
		appContext = (AppContext) getApplication();
		appVersionText.setText("版本：" + appContext.getVersionName());
	}

	private void initView() {
		copyrightText = (TextView) findViewById(R.id.copyright);
		copyrightText.setOnClickListener(this);
		titleText = (TextView) findViewById(R.id.title_bar_text);
		titleText.setText(R.string.app_about);
		appVersionText = (TextView) findViewById(R.id.app_version);
		checkUpdateBtn = (Button) findViewById(R.id.check_update);
		checkUpdateBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == copyrightText) {
			Uri uri = Uri.parse("http://blog.csdn.net/oinux");
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(intent);
		} else if (v == checkUpdateBtn) {
			checkUpdateBtn.setText(R.string.check_now);
			UmengUpdateAgent.forceUpdate(this);
			UmengUpdateAgent.setUpdateOnlyWifi(false);
			UmengUpdateAgent.setUpdateAutoPopup(false);
			UmengUpdateAgent.setUpdateListener(new UmengUpdateListener() {
				@Override
				public void onUpdateReturned(int updateStatus,
						UpdateResponse updateInfo) {
					switch (updateStatus) {
					case 0: // has update
						UmengUpdateAgent.showUpdateDialog(AppAbout.this,
								updateInfo);
						checkUpdateBtn.setText("新版本:" + updateInfo.version);
						break;
					case 1: // has no update
						checkUpdateBtn
								.setText(R.string.already_lastest_version);
						break;
					case 2: // none wifi
						break;
					case 3: // time out
						break;
					}
				}
			});
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
